﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreLib.MySQL
{
    public class CastableDbObject
    {
        internal string BaseString { get; private set; }
        internal CastableDbObject(object value)
        {
            BaseString = value.ToString();
        }

        public static implicit operator CastableDbObject(string value)
        {
            return new CastableDbObject(value);
        }

        public static implicit operator sbyte(CastableDbObject value)
        {
            sbyte val;
            if (sbyte.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator byte(CastableDbObject value)
        {
            byte val;
            if (byte.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator Int16(CastableDbObject value)
        {
            Int16 val;
            if (Int16.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator UInt16(CastableDbObject value)
        {
            UInt16 val;
            if (UInt16.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator Int32(CastableDbObject value)
        {
            Int32 val;
            if (Int32.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator UInt32(CastableDbObject value)
        {
            UInt32 val;
            if (UInt32.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator Int64(CastableDbObject value)
        {
            Int64 val;
            if (Int64.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator UInt64(CastableDbObject value)
        {
            UInt64 val;
            if (UInt64.TryParse(value.BaseString, out val))
                return val;
            throw new InvalidCastException();
        }

        public static implicit operator byte[](CastableDbObject value)
        {
            return Encoding.ASCII.GetBytes(value.BaseString);
        }

        public static implicit operator bool(CastableDbObject value)
        {
            return value == 1;
        }

        public static implicit operator string(CastableDbObject value)
        {
            return value.BaseString;
        }

        public override string ToString()
        {
            return BaseString;
        }
    }
}
